package com.lachesis.molecule.specification;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.lachesis.molecule.common.util.HttpRequestUtil;
import com.lachesis.molecule.specification.restful.RestApiScanner;

/** 
 * @ClassName: ApiMonitor 
 * @Description: Api规范监测器
 * @author shiwei.hu shiwei.hu@lachesis-mh.com
 * @date 2017年10月31日 下午7:49:48 
 *  
 */
public class ApiMonitor
{

	/** 
	 * @Fields apiMeetsRegulation : 通过扫描后，结果声明是否满足Api规范
	 */ 
	private boolean apiMeetsRegulation;	

	public boolean isApiMeetsRegulation() {
		return apiMeetsRegulation;
	}

	/** 
	 * @Fields LOG : 日志记录器 
	 */ 
	public static Logger LOG = Logger.getLogger(ApiMonitor.class);

	
	/** 
	 * @Title: startMonitor 
	 * @Description: 开始监测 
	 * @return void    返回类型 
	 * @throws 
	 */
	public void startMonitor() {

		// 防止启动两次


		LOG.info("正在检查服务RestApi规范...");


		List<ApiScanner> scannerList = new ArrayList<ApiScanner>();
		
		LOG.info("读取Api接口文档定义...");
//		LOG.info(HttpRequestUtil.sendGet("http://localhost:9098/ldm/v2/api-docs", null));
//		LOG.info("---------------------------------------------------------------------------------");

		scannerList.add(new RestApiScanner(HttpRequestUtil.sendGet("http://localhost:9098/ldm/v2/api-docs", null)));

		this.apiMeetsRegulation = true;
		for(ApiScanner scanner : scannerList) {
			this.apiMeetsRegulation = this.apiMeetsRegulation && scanner.Scan();
		}


		if(this.isApiMeetsRegulation()) {
			LOG.info("服务RestApi规范检查完成并已通过.");
		}
		else {        		
			LOG.fatal("服务RestApi规范检查失败. 除Swagger接口文档以外的所有的用户请求将会返回Error.");
		}
	}

}
